<?php
/**
* @package Pages-and-Items (com_pi_pages_and_items)
* @version 1.5.7
* @copyright Copyright (C) 2006-2010 Carsten Engel. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* @author http://www.pages-and-items.com
*/

//no direct access
if(!defined('_VALID_MOS') && !defined('_JEXEC')){
	die('Restricted access');
}

//check if user has right to upload download
if(!$class_pi->checkUserActionRight(19)){
	echo '<span style="font-size: 0.7em; font-family: arial;">'._pi_lang_nouploaddownload.'</span>';
	exit();
}

?>
<link href="components/com_pi_pages_and_items/css/popup.css" rel="stylesheet" type="text/css" />
<link href="components/com_pi_pages_and_items/css/pages_and_items3.css" rel="stylesheet" type="text/css" />
<?php
if( !defined('_JEXEC') ){//joomla version is 1.0.x so also get those red-like colors
	echo '<link href="components/com_pi_pages_and_items/css/joomla_1_0_x.css" rel="stylesheet" type="text/css" />';
}  
?>
<style type="text/css">

table.adminlist td{
	vertical-align: top;
}

body{
	margin-right: 5px;
}

#minwidth{
	min-width: 700px;
}

</style>
<script language="JavaScript" type="text/javascript">
<!--

function select_download(id){
	url = "index.php?option=com_pi_pages_and_items&task=download&id="+id;
	window.parent.document.getElementById('href').value = url;
	window.parent.document.getElementById('targetlist').options[0].selected = true;
	window.parent.document.getElementById('ispopup').checked = false;
	window.parent.document.getElementById('title').value = '';
}

function delete_download(id){
	if(confirm("<?php echo _pi_lang_suredeletedownloadfile; ?>")){
		document.getElementById('file_to_delete').value = id;
		document.adminForm.submit(); 	
	}
}

function reset_hits(id){
	if(confirm("<?php echo _pi_lang_surereset; ?>")){
		document.getElementById('file_to_reset').value = id;
		document.getElementById('task').value = 'download_reset';
		document.adminForm.submit(); 	
	}
}

// set language for link popup
function set_language_in_popup(){
	window.parent.document.getElementById('link_to_other_page').innerHTML = '<?php echo _pi_lang_link_to_other_page; ?>';
	window.parent.document.getElementById('link_to_download').innerHTML = '<?php echo _pi_lang_link_to_download; ?>';
	window.parent.document.getElementById('mail_link').innerHTML = '<?php echo _pi_lang_mail_link; ?>';
	window.parent.document.getElementById('extra_options').innerHTML = '<?php echo _pi_lang_extra_options; ?>';
	window.parent.document.getElementById('link_to_other_page2').innerHTML = '<?php echo _pi_lang_link_to_other_page2; ?>';
	window.parent.document.getElementById('upload_a_download').innerHTML = '<?php echo _pi_lang_upload_a_download; ?>';
	window.parent.document.getElementById('select_a_download').innerHTML = '<?php echo _pi_lang_select_a_download; ?>';
	window.parent.document.getElementById('email_link').innerHTML = '<?php echo _pi_lang_email_link; ?>';
	window.parent.document.getElementById('email_adress').innerHTML = '<?php echo _pi_lang_email_adress; ?>';
	window.parent.document.getElementById('subject').innerHTML = '<?php echo _pi_lang_subject; ?>';	
	window.parent.document.getElementById('make_email_link').value = '<?php echo _pi_lang_make_email_link; ?>';	
}
document.onload = setTimeout( "set_language_in_popup()" , 200);

function download_access_save(){
	document.getElementById('task').value = 'download_access_save';
	submitform('download_access_save');
}


<?php

if($class_pi->get_var('download_deleted', '' )==1){
	echo "window.parent.document.getElementById('href').value = '';";
	echo "window.parent.document.getElementById('targetlist').options[0].selected = true;";
	echo "window.parent.document.getElementById('ispopup').checked = false;";
	echo "window.parent.document.getElementById('title').value = '';";
}

?>

-->
</script>

<form name="adminForm" method="post" style="margin: 0; padding: 0;">
	<input type="hidden" name="option" value="com_pi_pages_and_items" />
	<input type="hidden" name="task" id="task" value="download_delete" />
	<input type="hidden" name="file_to_delete" id="file_to_delete" value="" />
	<input type="hidden" name="file_to_reset" id="file_to_reset" value="" />

<?php

//give headers in Joomla 1.5 a bit more spunk
if( defined('_JEXEC') ){
	//joomla 1.5
	echo '<style>
	th{		
		background: #F0F0F0;
		border-bottom: 1px solid #999999;
	}
	
	</style>';
}

//get allowed extensions in array
$allowed_extensions = explode(" ", $class_pi->pi_config['download_filetypes']);

//make list of downloads
$class_pi->db->setQuery("SELECT id, file_name, access, hits FROM #__pi_downloads ORDER BY file_name ASC"  );
$rows = $class_pi->db -> loadObjectList();

echo '<table class="adminlist" style="margin-top: 10px;">';
echo '<tr>';
echo '<th style="padding-left: 26px;">'._pi_lang_file.'</th>';

/*
if($class_pi->pi_config['download_display_access']==true){
	echo '<th style="text-transform: lowercase;">'._pi_lang_access.'&nbsp;';
	if($class_pi->pi_config['download_access_edit']==true && $class_pi->checkUserActionRight(22)){
		echo '<a href="javascript: download_access_save();"><img src="images/filesave.png" style="border: 0;" alt="save" /></a>';
	}
	echo '</th>';
}
*/

if($class_pi->pi_config['download_hits']==true){
	echo '<th style="text-align: right;">'._pi_lang_hits.'</th>';
}
//check if user has right to delete download
if($class_pi->checkUserActionRight(20)){
	echo '<th style="text-align: right">'._pi_lang_delete.'</th>';
}
echo '</tr>';
$k = 1;	
$extension = '';
foreach($rows as $row){	
	//only show the files with the right extension
	$extension = substr(strrchr($row->file_name, "."), 1);
	if(in_array($extension, $allowed_extensions)){
		echo '<tr class="row'.$k.'"><td>';
		$checked = '';
		if($row->id==$class_pi->get_var('id', '')){
			$checked = ' checked="checked"';			
		}
		echo '<label onclick="select_download('.$row->id.');">';
		echo '<input name="file" type="radio" class="radio" value="'.$row->id.'" '.$checked.' />';
		echo $row->file_name;
		echo '</label>';	
		/*
		echo '</td>';
		
		if($class_pi->pi_config['download_display_access']==true){
			echo '<td valign="top" style="width: 150px">';
			
			
			if($class_pi->pi_config['download_access_edit']==true && $class_pi->checkUserActionRight(22)){
				echo '<select name="access[]" class="inputbox">';
				$selected_0 = '';
				$selected_1 = '';
				$selected_2 = '';
				if($row->access==2){
					$selected_2 = 'selected="selected"';
				}else if($row->access==1){
					$selected_1 = 'selected="selected"';
				}else{
					$selected_0 = 'selected="selected"';
				}
				echo '<option value="'.$row->id.'__0" '.$selected_0.'>'._pi_lang_public.'</option>';
				echo '<option value="'.$row->id.'__1" '.$selected_1.'>'._pi_lang_logged.'</option>';
				echo '<option value="'.$row->id.'__2" '.$selected_2.'>'._pi_lang_special.'</option>';
				echo '</select>';
			}else{
				echo $class_pi->translate_accesslevel($row->access);
			}
			echo '</td>';
			
			
		}
		echo '</td>';
		*/
		if($class_pi->pi_config['download_hits']==true){
			echo '<td style="text-align: right;">'.$row->hits;			
			if($class_pi->pi_config['download_hitsreset']==true && $class_pi->checkUserActionRight(21)){
				echo ' <a href="javascript: reset_hits('.$row->id.');">'._pi_lang_reset.'</a>';
			}
			echo '</td>';
		}	
		//check if user has right to delete download
		if($class_pi->checkUserActionRight(20)){	
			echo '<td valign="top" style="text-align: right; width: 150px;">';		
			echo '<a href="javascript: delete_download('.$row->id.');">'._pi_lang_delete.'</a>';	
			echo '</td>';
		}
		echo '</tr>';
		if($k==1){
			$k = 0;
		}else{
			$k = 1;
		}	
	}
	
}
echo '</table>';

//if no downloads are there, display message
if($extension==''){
	echo '<div style="margin-top: 20px;text-align: center;">'._pi_lang_nodownloads.'</div>';
}
	
?>
</form>